<?php
namespace api\modules\v1\controllers;

use api\common\controllers\AController;
use common\records\Record;
use common\records\ToyMachine;
use common\records\UserInfo;
use common\records\Order;
use yii\data\ActiveDataProvider;

class OrderController extends AController
{
    public $modelClass = 'common\records\Order';
    /**
     * @api {get} /v1/orders 订单列表
     * @apiVersion 1.0.0
     * @apiName index
     * @apiGroup orders 
     *
     *
     */
    public function actionIndex()
    {
        if(!array_key_exists("uid",$_GET) )
        {
            $result = array();
            $detail = array();

            $detail['code'] = 400;
            $detail['reason'] = "fields missing";
            $result['result'] = $detail;
            return $result;
        }
        $uid = trim($_GET['uid']);
        $orders = Order::find()->where(['uid'=>$uid])->orderBy('order_id DESC')->asArray()->all();
        if(count($orders) > 0)
        {
            $order_id = 0;
            for($i = 0;$i<count($orders);$i++)
            {
                //if($orders[$i]['timestamp'])
                $order_time = strtotime($orders[$i]['timestamp']);
                if( (time() - $order_time< 90 && $orders[$i]['status'] == 0))
                {
                    $order_id = $orders[$i]['order_id'];
                    break;
                }
            }
            if($order_id == 0)
            {
                $record = Record::find()->where(['uid'=>$uid,'order_id'=>$orders[0]['order_id']])->one();

                if($record != null && $record->is_read == 0)
                {
                    $order_id = $orders[0]['order_id'];
                }
            }
            if($order_id > 0)
            {
                $result = array();
                $detail = array();
                $detail['code'] = 200;
                $detail['reason'] = "success";
                $detail['order_id'] = $order_id;

                $result['result'] = $detail;
                return $result;
            }
        }
        $result = array();
        $detail = array();
        $detail['code'] = 201;
        $detail['reason'] = "no unfinish order";

        $result['result'] = $detail;

        return $result;
    }
    public function actionCreate()
    {
        if(!array_key_exists("uid",$_POST) || !array_key_exists("access_token",$_POST) || !array_key_exists("machine_id",$_POST))
        {
            $result = array();
            $detail = array();
            $detail['code'] = 400;
            $detail['reason'] = "fields missing";

            $result['result'] = $detail;
            return $result;
        }

        $uid = trim($_POST['uid']);
        $access_token = trim($_POST['access_token']);
        $machine_id = trim($_POST['machine_id']);

        $user= UserInfo::find()->where(['uid'=>$uid,'access_token'=>$access_token])->one();
        if($user == null)
        {
            $result = array();
            $detail = array();
            $detail['code'] = 400;
            $detail['reason'] = "uid and access_token are not match";

            $result['result'] = $detail;
            return $result;
        }
        $machine_info = ToyMachine::find()->where(['mid'=>$machine_id])->one();
        if($machine_info == null)
        {
            $result = array();

            $detail = array();
            $detail['code'] = 404;
            $detail['reason'] = "unknown machine_id";

            $result['result'] = $detail;
            return $result;

        }

        if($user->coin < $machine_info->coin && $user->role != 1)
        {
            $result = array();
            $detail = array();
            $detail['code'] = 400;
            $detail['reason'] = "娃娃币不足";

            $result['result'] = $detail;
            return $result;
        }

        $order_count= Order::find()->where(['uid'=>$uid])->count();
        $order_info = new Order();
        $order_info->uid =  $uid;
        $order_info->machine_id = $machine_id;

        $order_info->coin = $machine_info->coin;
        $order_info->status = 0;//初始化
        $order_info->save();
        $order_id = $order_info->order_id;

        $user->coin = $user->coin - $machine_info->coin;
        $user->save(false);

        $result = array();
        $detail = array();
        $detail['code'] = 200;
        $detail['reason'] = "下单成功";
        $detail['order_id']= $order_id;
        if($order_count == 0)
        {
            $detail['power']= 40;
        }
        $result['result'] = $detail;
        return $result;

    }
}
